package com.mijie.homi.admin.service.reports;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.core.ApplicationProperties;
import com.mijie.core.Paging;
import com.mijie.homi.admin.dao.reports.ProsecuteDao;
import com.mijie.homi.admin.listener.IntegrationHandler;
import com.mijie.homi.admin.service.notify.NotifyUserAgent;
import com.mijie.homi.admin.service.user.UserService;
import com.mijie.homi.common.domain.reports.Prosecute;

@Service
public class ProsecuteService {

	@Autowired
	private ProsecuteDao dao;
	@Autowired
	private UserService userService;
	@Autowired
	private IntegrationHandler integrationHandler;
	@Autowired
	private NotifyUserAgent notifyUserAgent;
	
	private final Map<Integer,String> tableMap;
	
	public ProsecuteService(){
		tableMap = new HashMap<Integer,String>();
		tableMap.put(Prosecute.USER, "user");
		tableMap.put(Prosecute.MOOD_COMMENT, "moodReview");
		tableMap.put(Prosecute.TOPIC_OPINION, "topicOpinion");
		tableMap.put(Prosecute.TOPIC_OPINION_COMMENT, "topicOpinionReview");
		tableMap.put(Prosecute.PHOTO, "photo");
		tableMap.put(Prosecute.PHOTO_COMMENT, "photoReview");
		tableMap.put(Prosecute.MOOD_STORY, "mood");
	}
	
	public void listProsecute(Paging<Prosecute> paging,Map<String,Object> parameter){
		dao.listProsecute(paging,parameter);
	}
	
	public void batchHandle(int[] ids) throws Exception{
		int[] succIds = new int[ids.length];
		List<Prosecute> list = dao.getProsecuteById(ids);
		int rows = 0;
		int index = 0;
		String basePath = ApplicationProperties.get("resource.path");
		File smallFile = null;
		File largeFile = null;
		for(Prosecute prosecute : list){
			rows = handle(prosecute);
			if(rows>0){
				succIds[index++]=prosecute.getId();
				if(prosecute.getModule()==Prosecute.PHOTO){
					smallFile = new File(basePath+prosecute.getContent());
					largeFile = new File(basePath+prosecute.getContent().replaceFirst("small", "large"));
					if(smallFile.exists() && smallFile.isFile() && smallFile.getName().endsWith("jpg"))
						smallFile.delete();
						
					if(largeFile.exists() && largeFile.isFile() && largeFile.getName().endsWith("jpg"))
						largeFile.delete();
				}
			}else{
				//查看一下是否已经给主人删除
				if(!contentExists(prosecute))
					succIds[index++]=prosecute.getId();
			}
		}
		if(index>0) markHandled(succIds);
	}
	
	private int handle(Prosecute prosecute) throws Exception{
		String tableName = tableMap.get(prosecute.getModule());
		int rows = 0;
		if(tableName!=null){
			if(prosecute.getModule() == Prosecute.USER){
				userService.block(prosecute.getDefendant());
			}
			rows =  dao.handleProsecute(tableName, prosecute.getTargetId());
			if(rows>0){
				Integer reward  = integrationHandler.prosecuteSuccess(prosecute.getPlaintiff());
				notifyUserAgent.sendProsecuteHandleNotify(prosecute,true,reward);
			}
		}
		return rows;
	}
	
	/**
	 * 被举报的内容是否还存在
	 * @param prosecute
	 * @return
	 */
	public boolean contentExists(Prosecute prosecute){
		String tableName = tableMap.get(prosecute.getModule());
		if(tableName!=null){
			return dao.contentExists(tableName, prosecute.getTargetId());
		}
		return false;
	}
	
	private int markHandled(int[] ids){
		return dao.markHandled(ids);
	}
	
	public int ignoreProsecute(int[] ids){
		List<Prosecute> list = dao.getProsecuteById(ids);
		for(Prosecute prosecute : list){
			notifyUserAgent.sendProsecuteHandleNotify(prosecute,false,null);
		}
		return markHandled(ids);
	}
	
	public int deleteProsecute(int[] id){
		return dao.deleteProsecute(id);
	}
}
